<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class mads extends CI_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "ads";
    public $primaryKey = "ADS_ID";
    public $resCode = "ADS";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
		$fieldData['ADS_CODE'] = '';
		$fieldData['ADS_TITLE'] = '';
		$fieldData['ADS_POSITION'] = '';
		$fieldData['ADS_HTML'] = '';
		$fieldData['ADS_TIME_START'] = time();
		$fieldData['ADS_TIME_END'] = time();
		$fieldData['ADS_STATUS'] = 1;
		$fieldData['ADS_IS_SCROLL'] = 1;
		$fieldData['ADS_ORDER'] = 0;
		return $fieldData;
    }

    function get($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
        return $this->db->get()->first_row();
    }

    function gets($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
        return $this->db->get()->result();
    }

    function where_in($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        $this->db->from($this->mytable);
        if ($val != '')
            $this->db->where_in($this->mytable . "." . $param, $val);
        return $this->db->get()->result();
    }

    function num($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        $this->db->from($this->mytable);
        $this->db->where($param, $val);
        return $this->db->count_all_results();
    }

    function update($val = '', $param = null, $arr_update) {
        if ($param == null)
            $param = $this->primaryKey;

        if ($val != '')
            $this->db->where($param, $val);
		//$arr_update['OS_TIME_UPDATE'] = time();
        $arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
    }

    function insert($arr_insert) {
        $arr_insert = $this->msystem->insert_security($arr_insert, $this->field());
        $this->db->insert($this->mytable, $arr_insert);
        return $this->db->insert_id();
    }

    function delete($val = '', $param = null) {
        if ($val != '') {
            $this->db->where($param, $val);
            $this->db->delete($this->mytable);
        }
    }
	
	function getsAds($ads_code = '',$ads_pos = ''){
		$condition = array();
	
		if($ads_code !=''){
			$condition['ADS_CODE'] = $ads_code;
		}
		if($ads_pos !=''){
			$condition['ADS_POSITION'] = $ads_pos;
		}
		$this->db->order_by("ADS_ORDER asc");
		$this->db->where("ADS_STATUS",1);
		return $this->gets($condition);
	}
	
	function ads($ads_pos = '',$ads_code = '') {
	$adss = $this->getsAds($ads_code,$ads_pos);
	$html = '';
	if($adss) {
		foreach($adss as $ads){
			if($ads->ADS_IS_SCROLL ==1) {
						$html.= '<div class="summary">'.$ads->ADS_HTML.'</div>';
					}else{
						$html.= $ads->ADS_HTML;
				}
		}
	}
	if($ads_code && $ads_code != ''){
		$adss = $this->getsAds('all-page',$ads_pos);
		if($adss) {
			foreach($adss as $ads){
				if($ads->ADS_IS_SCROLL ==1) {
						$html.= '<div class="summary">'.$ads->ADS_HTML.'</div>';
					}else{
						$html.= $ads->ADS_HTML;
				}
			}
		}
	}
	return $html;
	}
}

?>